package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.Response;
import com.baidu.location.BDLocation;
import com.fqcar.fqdf.DFApp;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.adapter.HistoryCarListAdapter;
import com.fqcar.fqdf.adapter.StartDealReasonAdapter;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.entity.HistoryCarListBean;
import com.fqcar.fqdf.entity.StartDealRespBean;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqdf.framework.android.net.response.NetBaseWrapper;
import com.fqdf.framework.android.net.response.NetBeanWrapper;
import com.fqdf.framework.android.util.Constants;
import com.fqdf.framework.android.view.NoScrollGridLayoutManager;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;
import com.fqdf.framework.android.view.recycleviewdivider.GridSpacingDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lvsidong
 * @version V1.0
 * @date 2017/10/17
 */
public class StartDealActivity extends BaseRequestActivity implements RecycleItemClickListener {


    @BindView(R.id.cancel_reason)
    RecyclerView cancelReason;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.list)
    ListView list;

    private int curClickPosition;

    private StartDealReasonAdapter startDealReasonAdapter;
    private HistoryCarListAdapter historyCarListAdapter;
    private List<String> reasonList = new ArrayList<>();
    private List<HistoryCarListBean> historyCarList = new ArrayList<>();

    private int carId;
    private BDLocation location = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_deal);
        ButterKnife.bind(this);

        carId = getIntent().getIntExtra(Constants.BUNDLE_DATA, 0);
        getReasonList(carId);
        initMyView();
    }

    /**
     * 初始化View
     */
    private void initMyView() {
        initRecycleView();

        location = ((DFApp) getApplication()).getCurLocation();

    }


    /**
     * 初始化recycleview控件以及数据
     */
    private void initRecycleView() {

        NoScrollGridLayoutManager gridManager = new NoScrollGridLayoutManager(this, 2);
        cancelReason.setLayoutManager(gridManager);

        startDealReasonAdapter = new StartDealReasonAdapter(reasonList, this);
        cancelReason.setAdapter(startDealReasonAdapter);
        cancelReason.addItemDecoration(new GridSpacingDecoration(getResources().getDimensionPixelOffset(R.dimen.dimen_15_dip),
                getResources().getDimensionPixelOffset(R.dimen.dimen_12_dip), false));
        historyCarListAdapter = new HistoryCarListAdapter(this);
        list.setAdapter(historyCarListAdapter);

    }

    @Override
    public void onItemClick(View view, int position) {
//        new PickAndCameraPhotoDialog(StartDealActivity.this).show();
//        curClickPosition = position;
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }

    @OnClick({R.id.back_iv, R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
//                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.confirm:
                submitData();
                break;
        }
    }

    public void setBackground() {
        confirm.setEnabled(true);
        confirm.setBackgroundResource(R.drawable.btn_main_new_darkgreen);
    }

//    @Override
//    public void onBackPressed() {
//        setResult(RESULT_CANCELED);
//        finish();
//    }

    /**
     * 提交数据请求
     */
    private void submitData() {
        String reason = startDealReasonAdapter.getReasonText();

        commitStartOrder(carId, reason, location.getLatitude(), location.getLongitude());

    }

    private void getReasonList(int carId) {
        RequestOptService.getStartDealPage(this, carId,new Response.Listener<NetBeanWrapper<StartDealRespBean>>() {
            @Override
            public void onResponse(NetBeanWrapper<StartDealRespBean> response) {
                if (handleResponse(response)) {
                    return;
                }

                reasonList = response.getModelData().getCauseList();
                historyCarList = response.getModelData().getDealList();

                startDealReasonAdapter.setList(reasonList);
                startDealReasonAdapter.notifyDataSetChanged();

                historyCarListAdapter.setList(historyCarList);
                historyCarListAdapter.notifyDataSetChanged();
            }
        }, getDefaultErrorListener());
    }


    private void commitStartOrder(final int carId, String cause, double lat, double lng) {
        RequestOptService.startDeal(StartDealActivity.this, carId, cause, lat, lng, new Response.Listener<NetBaseWrapper>() {
            @Override
            public void onResponse(NetBaseWrapper response) {
                if (!handleResponse(response)) {
                    Intent intent = new Intent(StartDealActivity.this, CarDetailActivity.class);
                    intent.putExtra(Constants.BUNDLE_DATA, carId);
                    StartDealActivity.this.startActivity(intent);
                }
            }
        }, getDefaultErrorListener());
//        RequestOptService.cancelOrderInfo(getApplicationContext(), reqBean, new Response.Listener<NetBaseWrapper>() {
//            @Override
//            public void onResponse(NetBaseWrapper response) {
//                if (!handleResponse(response)) {
//
//                    DialogUtil.ToastMessage(ReportingProblemsActivity.this, R.string.order_cancel_ok);
//                    Intent intent = new Intent(ReportingProblemsActivity.this, NormalHomeActivity.class);
//                    intent.setAction(Constants.Action.HOME_REFRESH_ACTION);
//                    startActivity(intent);
//                    setResult(RESULT_OK);
//                    finish();
//                } else {
//                    DialogUtil.hideIndeterminateProgress(getNoCancelSubmitDialog());
//                }
//            }
//        }, getHidenProgressErrorListener());
    }
}
